﻿using System;
using System.Collections;
using MyProFile_V2.Model;
using System.Data;
using System.Data.SqlClient;
namespace MyProFile_V2.Data
{
    public class TableService
    {

        public static ArrayList getTables()
        {
            ArrayList al = new ArrayList();
            string sqlStr = "select [name],[id],[xtype] from sysobjects where xtype='u' or xtype='v'"; //or xtype='v'";
            DataTable dt = DBHelper.QueryDataTable(sqlStr);
            string sqlTabDes = "select value  from sys.extended_properties where major_id=@ID and minor_id=0";

            foreach (DataRow row in dt.Rows)
            {


                TableModel tm = new TableModel();
                string strtm = row["xtype"].ToString().ToLower().Trim();
                if ("v".Equals(strtm))
                {//如果是视图
                    tm.IsView = true;
                }
                else
                {
                    tm.IsView = false;
                }
                tm.Name = row["name"].ToString();
                if (tm.Name.StartsWith("sys"))
                {//如果是系统表
                    continue;
                }
                tm.Id = row["id"].ToString();
                tm.Columns = ColumnService.getColumnById(tm.Id);
                //表的描述
                SqlParameter[] parmas = new SqlParameter[]{
                new SqlParameter("@ID",tm.Id)
                };
                DataTable dtTabDes = DBHelper.QueryDataTable(sqlTabDes, parmas);
                if (dtTabDes.Rows.Count > 0)
                {
                    DataRow rowz = dtTabDes.Rows[0];
                    tm.TabDes = Convert.ToString(rowz["value"]);
                }
                al.Add(tm);
            }
            return al;
        }
    }
}
